$(function() {
	BondClearingTableTable();

});

var BondClearingTableTable = function() {

	layui
			.use(
					[ 'laydate', 'laypage', 'layer', 'table', 'element',
							'upload' ],
					function() {
						var laydate = layui.laydate // 日期
						, laypage = layui.laypage // 分页
						, layer = layui.layer // 弹层
						, table = layui.table // 表格
						, element = layui.element// 元素操作
						, upload = layui.upload;
						var form = layui.form;
						var $ = layui.jquery;
						// 执行一个 table 实例
						var bondClearingTable = table
								.render({
									elem : '#BondClearingTable',
									url : 'BondTrade',// 数据接口
									title : '债券交易',
									page : true,// 开启分页
									toolbar : '#liquidation' // 开启头部工具栏，并为其绑定左侧模板
									,
									defaultToolbar : [ 'filter', 'exports',
											'print', ],
									limits : [ 5, 10, 15 ],
									cols : [ [ // 表头
											{
												type : 'checkbox',
											},
											{
												field : 'bondTradeId',
												title : '债券交易ID',
												width : 200,
												hide : true
											},
											{
												field : 'bondTradeNo',
												title : '债券交易编号',
												width : 200,
											},
											{
												field : 'fundNo',
												title : '基金代码',
												width : 100,
											},
											{
												field : 'fundName',
												title : '基金名',
												width : 200,
											},
											{
												field : 'bondNo',
												title : '债券代码',
												width : 200,
											},
											{
												field : 'bondName',
												title : '债券名',
												width : 100,
											},
											{
												field : 'userName',
												title : '基金经理名',
												width : 100,
											},
											{
												field : 'brokerName',
												title : '券商名',
												width : 100,
											},
											{
												field : 'tradeDeskName',
												title : '交易席位名',
												width : 100,
											},
											{
												field : 'tradeInOrOut',
												title : '交易方式',
												width : 100,
												templet : function(d) {
													if (d.tradeInOrOut == 1) {
														return '买入'
													} else if (d.tradeInOrOut == 2) {
														return '卖出'
													} else if (d.tradeInOrOut == 3) {
														return '分红'
													} else if (d.tradeInOrOut == 4) {
														return '派股'
													}
												}
											},
											{
												field : 'tradeFlag',
												title : '交易标识',
												width : 100,
												templet : function(d) {
													if (d.tradeFlag == 1) {
														return '流入'
													} else {
														return '流出'
													}
												}
											},
											{
												field : 'tradePrice',
												title : '交易价格',
												width : 100,
											},
											{
												field : 'tradeDate',
												title : '交易时间',
												width : 150,
												templet : function(data) {
													var tradeDate = new Date(
															data.tradeDate);
													var minutes = tradeDate
															.getMinutes();
													if (minutes < 10) {
														minutes = "0" + minutes
													}
													return tradeDate
															.getFullYear()
															+ "-"
															+ (tradeDate
																	.getMonth() + 1)
															+ "-"
															+ tradeDate
																	.getDate()
															+ " "
															+ tradeDate
																	.getHours()
															+ ":" + minutes;
												}
											},
											{
												field : 'share',
												title : '交易数量',
												width : 100,
											},
											{
												field : 'turnvoer',
												title : '交易额',
												width : 100,
											},
											{
												field : 'stampTax',
												title : '印花税',
												width : 100,
											},
											{
												field : 'managementFees',
												title : '征管费',
												width : 100,
											},
											{
												field : 'trabsferFee',
												title : '过户费',
												width : 100,
											},
											{
												field : 'commission',
												title : '佣金费用',
												width : 100,
											},
											{
												field : 'brokerage',
												title : '经手费',
												width : 100,
											},
											{
												field : 'total',
												title : '总金额',
												width : 100,
											},
											{
												field : 'securityInterest',
												title : '债券利息',
												width : 100,
											},
											{
												field : 'tradeStatus',
												title : '交易状态',
												width : 100,
												templet : function(d) {
													if (d.tradeStatus == 1) {
														return '<span style="color:green;">'
																+ '已结算'
																+ '</span>'
													} else {
														return '<span style="color:red;">'
																+ '未结算'
																+ '</span>'
													}
												}
											}

									] ]
								});
						form.on('submit(BondClearingSearchBtn)',
								function(data) {
									var data = data.field
									bondClearingTable.reload({
										where : { // 设定异步数据接口的额外参数，任意设
											"startTime" : data.startTime,
											"endTime" : data.endTime,
										},
										page : {
											curr : 1
										// 重新从第 1 页开始
										}
									});
									if (data.startTime <= data.endTime) {

									} else {
										layer.msg('结束时间小于起始时间,请重新选择');
									}
									return false; // 阻止表单跳转。如果需要表单跳转，去掉这段即可。
								});
						// 日期时间选择器
						laydate.render({
							elem : '#startTime1',
							type : 'datetime'
						});
						laydate.render({
							elem : '#endTime1',
							type : 'datetime'
						});
						// 头工具栏事件
						table.on('toolbar(BondClearingTable)', function(obj) {
							var checkStatus = table.checkStatus(obj.config.id);
							switch (obj.event) {
							case 'liquidation':
								var data = checkStatus.data;
								closeAnAccount(data);
								break;
							case 'theLiquidation':
								var data = checkStatus.data;
								againstCloseAnAccount(data);
								break;
							}
							;
						});
						// 清算
						var closeAnAccount = function(data) {
							var bondTradeId = '';
							var tradeStatus = '';
							for (var i = 0; i < data.length; i++) {
								bondTradeId += data[i].bondTradeId + ',';
								tradeStatus += data[i].tradeStatus + ',';
							}
							
							
							var s3 = '';
							var festival = [ "1-1", "2-4", "2-5", "2-6", "2-7",
									"2-8", "2-9", "2-10", "4-5", "4-6", "4-7",
									"5-1", "5-2", "5-3", "5-4", "6-7", "6-8",
									"6-9", "9-13", "9-14", "9-15", "10-1",
									"10-2", "10-3", "10-4", "10-5", "10-6",
									"10-7" ];
							var day1 = new Date();
							var day2 = new Date();
							day1.setTime(day1.getTime());
							s2 = day1.getFullYear() + "-"
									+ (day1.getMonth() + 1) + "-"
									+ day1.getDate();
							s3 = (day2.getMonth() + 1) + "-" + day2.getDate();
							if (data.length < 1) {
								layer.confirm('至少选择一条数据');
							} else if (data.length >= 1) {
								for (var i = 0; i < data.length; i++) {
									if (data[i].tradeStatus == 1) {
										layer.confirm('您选择的数据中存在已结算 请重新选择');
										return;
									}

									var stime = Date.parse(new Date(
											data[i].tradeDate));
									var etime = Date.parse(new Date(day1));
									// 两个时间戳相差的毫秒数
									var usedTime = etime - stime;
									// 计算相差的天数
									var days = Math.floor(usedTime
											/ (24 * 3600 * 1000));
									// 计算天数后剩余的毫秒数
									var leave1 = usedTime % (24 * 3600 * 1000);
									// 计算出小时数
									var hours = Math.floor(leave1
											/ (3600 * 1000));
									// 计算小时数后剩余的毫秒数
									var leave2 = leave1 % (3600 * 1000);
									// 计算相差分钟数
									var minutes = Math.floor(leave2
											/ (60 * 1000));
									var time = days + "天" + hours + "时"
											+ minutes + "分";
									/*if (days <= 1) {
										layer.confirm('请24小时后再清算');
										return;	
									}*/

								}
								for (var i = 0; i < getDate().length; i++) {
									if (getDate()[i] == s2) {
										layer.confirm('节假日或休息日不能结算');
										return;
									}
								}
								for (var i = 0; i < festival.length; i++) {
									if (festival[i] == s3) {
										layer.confirm('节假日或休息日不能结算');
										return;
									}
								}

								layer.load();
								$.ajax({
											type : "POST",
											url : 'BondCleareing',
											data : {
												_method : 'PUT',
												'bondTradeId' : bondTradeId,
												'tradeStatus' :tradeStatus
											},
											traditional : true,// 属性在这里设置
											dataType : 'json',
											success : function(data) {

												if (data == true) {
													layer.confirm(
																	'清算成功',
																	{
																		btn : [
																				'确定',
																				'取消' ]
																	},
																	function() {
																		setTimeout('window.location.reload()');
																	});
													layer.closeAll('loading');

												} else {
													layer.closeAll('loading');
													layer.confirm('清算失败');
												}

												// 刷新数据表格
												table.reload(
														'BondClearingTable', {
															url : 'BondTrade'
														});
											}
										})
							}

						}
						// 反清算
						var againstCloseAnAccount = function(data) {
							var bondTradeId = '';
							var tradeStatus = '';
							for (var i = 0; i < data.length; i++) {
								bondTradeId += data[i].bondTradeId + ',';
								tradeStatus += data[i].tradeStatus + ',';
							}
							
							var s3 = '';
							var festival = [ "1-1", "2-4", "2-5", "2-6", "2-7",
									"2-8", "2-9", "2-10", "4-5", "4-6", "4-7",
									"5-1", "5-2", "5-3", "5-4", "6-7", "6-8",
									"6-9", "9-13", "9-14", "9-15", "10-1",
									"10-2", "10-3", "10-4", "10-5", "10-6",
									"10-7" ];
							var day1 = new Date();
							var day2 = new Date();
							day1.setTime(day1.getTime());
							s2 = day1.getFullYear() + "-"
									+ (day1.getMonth() + 1) + "-"
									+ day1.getDate();
							s3 = (day2.getMonth() + 1) + "-" + day2.getDate();

							if (data.length < 1) {
								layer.confirm('至少选择一条数据');
							} else if (data.length >= 1) {
								for (var i = 0; i < data.length; i++) {
									if (data[i].tradeStatus == 0) {
										layer.confirm('您选择的数据中存在未结算 请重新选择');
										return;
									}

									var stime = Date.parse(new Date(
											data[i].tradeDate));
									var etime = Date.parse(new Date(day1));
									// 两个时间戳相差的毫秒数
									var usedTime = etime - stime;
									// 计算相差的天数
									var days = Math.floor(usedTime
											/ (24 * 3600 * 1000));
									// 计算天数后剩余的毫秒数
									var leave1 = usedTime % (24 * 3600 * 1000);
									// 计算出小时数
									var hours = Math.floor(leave1
											/ (3600 * 1000));
									// 计算小时数后剩余的毫秒数
									var leave2 = leave1 % (3600 * 1000);
									// 计算相差分钟数
									var minutes = Math.floor(leave2
											/ (60 * 1000));
									var time = days + "天" + hours + "时"
											+ minutes + "分";
									/*if (days <= 1) {
										layer.confirm('请24小时后再清算');
										return;	
									}*/
									

								}
								for (var i = 0; i < getDate().length; i++) {
									if (getDate()[i] == s2) {
										layer.confirm('节假日或休息日不能结算');
										return;
									}
								}
								for (var i = 0; i < festival.length; i++) {
									if (festival[i] == s3) {
										layer.confirm('节假日或休息日不能结算');
										return;
									}
								}
								layer.load();
								$
										.ajax({
											type : "POST",
											url : 'BondCleareing',
											data : {
												_method : 'PUT',
												'bondTradeId' : bondTradeId,
												'tradeStatus' :tradeStatus
											},
											traditional : true,// 属性在这里设置
											dataType : 'json',
											success : function(data) {
												if (data == true) {
													layer
															.confirm(
																	'成功',
																	{
																		btn : [
																				'确定',
																				'取消' ]
																	},
																	function() {
																		setTimeout('window.location.reload()');
																	});
													layer.closeAll('loading');
												} else {
													layer.closeAll('loading');
													layer.confirm('失败');
												}
												// 刷新数据表格
												table.reload(
														'BondClearingTable', {
															url : 'BondTrade'
														});
											}
										})
							}

						}

						var getDate = function() {
							let date = new Date();
							let year = date.getFullYear();
							let m, d, day, dayNum, result = "";
							var rs = [];
							var s2 = "";
							for (m = 1; m <= 12; m++) {
								switch (m) {
								case 1:
								case 3:
								case 5:
								case 7:
								case 8:
								case 10:
								case 12:
									dayNum = 31;
									break;
								case 4:
								case 6:
								case 9:
								case 11:
									dayNum = 30;
									break;
								case 2:
									if (year % 4 == 0 && year % 100 != 0
											|| year % 400 == 0) {
										dayNum = 29;
									} else {
										dayNum = 28;
									}
									break;
								}
								for (d = 1; d <= dayNum; d++) {
									date.setMonth(m - 1, d);
									day = date.getDay();
									if (day == 0) {
										result = [ date.getFullYear() + "-"
												+ (date.getMonth() + 1) + "-"
												+ date.getDate() ];
										rs.push(result);
									} else if (day == 6) {
										result = [ date.getFullYear() + "-"
												+ (date.getMonth() + 1) + "-"
												+ date.getDate() ];
										rs.push(result);
									}

									/*
									 * if(s2==result){ return
									 * console.log("今天是周末"); }else{ return
									 * console.log("今天不是周末"); }
									 */
								}

							}

							return rs;

						}

					})

}
